//添加购物车
function addCart(bid) {
    if(confirm("是否加入购物车？")){
        $.post("../shoppingCart/addCart.do",{bid:bid},function (data) {
            alert(data.errorInfo);
            if(data.errorType == 0){
                location.reload();
            }else if(data.errorType == 1){
                location.href="../user/login.do";
            }
        });
    }
}
//加载页面  获取图书信息
function load(start, btype, bname) {
    //发起请求获得书籍信息
    var books = $("#books");
    var divs = "";
    $.getJSON("../book/getBooks.do", {start: start, btype: btype, bname: bname}, function (data) {
        j_btype = btype;
        for (var i = 0; i < data.list.length; i++) { //遍历填充
            var str = data.list[i].author + "&nbsp;著，" + data.list[i].publishing;
            str = str.substring(0, 20) + "...";
            divs += "<div class=\"thumbnail\">\n" +
                "\t\t\t  <a href=\"../static/showBook.html?bid=" + data.list[i].bid + "\"><img src=\"../book/download?img=" + data.list[i].bigImg + "\" alt=\"图片加载失败...\" title=\"查看详情\" width='200px' height='200px'></a>\n" +
                "\t\t\t  <div class=\"caption\">\n" +
                "\t\t\t\t<h3><a href=\"../static/showBook.html?bid=" + data.list[i].bid + "\" class=\"bk_name\">" + data.list[i].bookName + "</a></h3>\n" +
                "\t\t\t\t<p class=\"author\">" + str + "</p>\n" +
                "\t\t\t\t<p class=\"price\">￥" + data.list[i].price + "</p>\n" +
                "\t\t\t\t<p><a href=\"javascript:;\" class=\"btn btn-primary\" role=\"button\" onclick='PayGoods()'>购买</a> " +
                "<a href=\"javascript:;\" class=\"btn btn-default\" role=\"button\" onclick='addCart(" + data.list[i].bid + ")'>加入购物车</a></p>\n" +
                "\t\t\t  </div>\n" +
                "\t</div>";
        }
        books.html(divs);//生成div
        //分页
        var lis = "<li>\n" +
            "                <a href=\"#\" aria-label=\"Previous\" onclick='load(" + data.prePage + ")'>\n" +
            "                    <span aria-hidden=\"true\">&laquo;</span>\n" +
            "                </a>\n" +
            "            </li>";
        if (data.prePage == 0) {
            lis = "<li class='disabled'>\n" +
                "                <a href=\"#\" aria-label=\"Previous\">\n" +
                "                    <span aria-hidden=\"true\">&laquo;</span>\n" +
                "                </a>\n" +
                "            </li>";
        }
        for (var i = 1; i <= data.lastPage; i++) {
            lis += "<li><a href=\"#\"  onclick='load(" + i + ")'>" + i + "</a></li>";
        }
        //结尾
        if (data.nextPage == 0) { //下一页大于最后一页时  按钮禁用
            lis += "<li class='disabled'>\n" +
                "                <a href=\"#\" aria-label=\"Next\" >\n" +
                "                    <span aria-hidden=\"true\">&raquo;</span>\n" +
                "                </a>\n" +
                "            </li>";
        } else {
            lis += "<li>\n" +
                "                <a href='#' onclick='load(" + data.nextPage + ")' aria-label=\"Next\">\n" +
                "                    <span aria-hidden=\"true\">&raquo;</span>\n" +
                "                </a>\n" +
                "            </li>";
        }
        //生成
        $("#pagination").html(lis);
        $("#tips").html("当前第" + data.pageNum + "页，共" + data.pages + "页，" + data.total + "条数据");
    });
}